package br.net.woodstock.tickettoride.client.common;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletRequest;

import br.net.woodstock.tickettoride.client.TicketConverter;

public class ServletRequestConverter implements TicketConverter<ServletRequest> {

	@Override
	public ServletRequest fromBytes(final byte[] bytes) {
		throw new UnsupportedOperationException();
	}

	@Override
	public byte[] toBytes(final ServletRequest request) {
		MapTicketConverter converter = new MapTicketConverter();
		Map<String, String> map = new HashMap<String, String>();
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			String name = enumeration.nextElement().toString();
			String value = request.getParameter(name);
			map.put(name, value);
		}
		byte[] data = converter.toBytes(map);
		return data;
	}

}
